package com.wjyf.reducing_bento.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjyf.reducing_bento.annotation.AuthCheck;
import com.wjyf.reducing_bento.common.BaseResponse;
import com.wjyf.reducing_bento.common.ErrorCode;
import com.wjyf.reducing_bento.common.ResultUtils;
import com.wjyf.reducing_bento.model.dto.LoginUserDto;
import com.wjyf.reducing_bento.model.dto.UserRegisterDto;
import com.wjyf.reducing_bento.model.entity.UserBean;
import com.wjyf.reducing_bento.service.UserService;
import io.swagger.annotations.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.wjyf.reducing_bento.constant.UserConstant.ADMIN;
import static com.wjyf.reducing_bento.constant.UserConstant.USER_SESSION_KEY;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wjyf
 * @since 2023-03-14 08:36:33
 */
@Api(value = "/user-bean", tags = {"用户 前端控制器"})
@RestController
@RequestMapping("/user-bean")
public class UserController {

    @Resource
    private UserService userService;

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "LoginUserDto", name = "loginUserDto", value = "登录需要传的对象", required = true)
    })
    @ApiOperation(value = "登录", notes = "", httpMethod = "POST")
    @PostMapping("/login")
    public BaseResponse login(@RequestBody LoginUserDto loginUserDto, HttpServletRequest request) {
        UserBean user = userService.login(loginUserDto, request);
        return ResultUtils.success(user);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "double", name = "money", value = "需要充值的钱", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "long", name = "userId", value = "需要充值的用户Id", required = true)
    })
    @ApiOperation(value = "充值", notes = "", httpMethod = "POST")
    @PostMapping("recharge")
    @AuthCheck(mustRole = ADMIN)
    public BaseResponse recharge(@RequestParam Double money, @RequestParam Long userId) {
        boolean isSuccess = userService.recharge(money, userId);
        return isSuccess ? ResultUtils.success(isSuccess) : ResultUtils.error(ErrorCode.OPERATION_ERROR, "充值失败");
    }


    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "long", name = "current", value = "当前页号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "long", name = "pageSize", value = "页面大小", required = true)
    })
    @ApiOperation(value = "获取用户列表", notes = "", httpMethod = "GET")
    @GetMapping("user-list")
    @AuthCheck(mustRole = ADMIN)
    public BaseResponse userList(@RequestParam Long current,
                                 @RequestParam Long pageSize,
                                 @RequestParam(required = false) @ApiParam("搜索关键词（昵称、手机号码）") String key) {
        Page<UserBean> userBeanPage = userService.getUserPage(current, pageSize, key);
        return ResultUtils.success(userBeanPage);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "UserRegisterDto", name = "userRegisterDto", value = "", required = true)
    })
    @ApiOperation(value = "注册", notes = "参数必传", httpMethod = "POST")
    @PostMapping("register")
    public BaseResponse register(@RequestBody UserRegisterDto userRegisterDto) {
        boolean isSuccess = userService.register(userRegisterDto);
        return isSuccess ? ResultUtils.success(isSuccess) : ResultUtils.error(ErrorCode.OPERATION_ERROR, "注册失败");
    }

    @GetMapping("/user-get-info")
    @ApiOperation("用户获取自己的个人信息")
    public BaseResponse userGetInfo(HttpServletRequest request){
        UserBean userBean = userService.getUserInfo(request);
        request.getSession().setAttribute(USER_SESSION_KEY, userBean);
        return ResultUtils.success(userBean);
    }
}
